//
//  DSOrderListViewModel.m
//  DSJY
//
//  Created by 冯华强 on 16/9/28.
//  Copyright © 2016年 dsjy. All rights reserved.
//  我的成绩VM

#import "DSOrderListViewModel.h"
#import "DSOrder.h"
#import "DSNetworkTool.h"
#import "DSAccountViewModel.h"
#import "YQResponse.h"
#import "DSOrderInfo.h"

@interface DSOrderListViewModel ()
//学习记录列表
@property (nonatomic, strong, readwrite) NSMutableArray<DSOrder *> *orderList;
@end

@implementation DSOrderListViewModel

#pragma mark - 懒加载
- (NSMutableArray<DSOrder *> *)orderList
{
    if (_orderList == nil) {
        _orderList = [NSMutableArray<DSOrder *> array];
    }
    return _orderList;
}

#pragma mark - 初始化
- (instancetype)init
{
    if (self = [super init]) {
        //绑定事件
        [self initialBind];
    }
    return self;
}

- (void)initialBind
{
    @weakify(self);
    //初始化数据
    [self createCmdSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
        @strongify(self);
        [DSNetworkTool studentOrderListWithParams:[DSAccountViewModel sharedAccount].account channel:DSOrderInfoChannelValue success:^(NSDictionary *responseObject) {
            //返回值
            self.response = [YQResponse mj_objectWithKeyValues:responseObject];
            [self veriResponse:self.response finishedCallBack:^{
                //列表
                NSArray<DSOrder *> *orderList = [DSOrder mj_objectArrayWithKeyValuesArray:responseObject[@"datas"]];
                
                if (self.pageNum == 1) {
                    [self.orderList removeAllObjects];
                }
                [self.orderList addObjectsFromArray:orderList];
                
                [subscriber sendNext:nil];
            }];
            [subscriber sendCompleted];
        } failure:^(NSError *error) {
            MTLog(@"%@", error);
            [SVProgressHUD showErrorWithStatus:@"系统异常!"];
            [subscriber sendCompleted];
        }];
        return nil;
    }];
    
    //设置请求时的提示框
    [self executingWithCommand:self.requestCommand];
}


@end
